<?php
/**
 * 新房
 * User: E 房cms www.fangcms.com
 * Date: 2015/1/20
 * Time: 10:51
 */
namespace Home\Controller;
use Org\Yike\FangCms;
class UsedController extends CommonController{
    public function _initialize(){
        parent::_initialize();
        $this->action = strtolower(ACTION_NAME);
        $this->news_list = M('article')->where(array('status'=>1,'cate_id'=>15))->limit(9)->order('id desc')->select();
    }

    public function index(){
        $FangCms = new FangCms();
        $menu = getLinkMenuFor(array(2,8,10,17,20,21,22,23,24),1);
        $houseArr = $FangCms->getUsedSearch($menu,true);
        $this->rec_used = $FangCms->getRecUsed(8);

        $this->area_cate = $houseArr['arr']['area_cate'];
        $this->cate_pid_info = $houseArr['arr']['cate_pid_info'];
        $this->cate_pid_list = $houseArr['arr']['cate_pid_list'];
        $this->menu = $menu;
        $this->url = $houseArr['url'];
        $this->s = $houseArr['s'];
        $this->list = $houseArr['arr']['list'];
        $this->page = $houseArr['arr']['page'];
        $this->houseArr = $houseArr;
        $seo = $houseArr['arr']['cate_info'];
        if($this->cate_pid_info  or $seo){
            if($this->cate_pid_info['id']!=$seo['id']){
                $name =  $this->cate_pid_info['name'].$seo['name'];
            }else{
                $name = $seo['name'];
            }
            $seo['seo_title'] = $seo['seo_keys'] =$name.'二手房_'.$name.'二手房出售信息';
            $seo['seo_desc'] = $this->siteName.$name."二手房频道为您提供大量的".$name."二手房出售、所有、交易、二手房转让，中介等信息";
            $this->seoKey($seo,'name');
        }
        $this->display();
    }

    //楼盘主页
    public function detail(){
        $resArr = $this->getUsed();
        $this->menu = getLinkMenuFor(array(10,17,21,20));
        $this->map = explode(',',$resArr['used']['map']);
        $this->tags = explode(',',$resArr['used']['tags']);
        $this->mating = explode('|',trim($resArr['used']['mating'],'|'));
        $estate_id = $resArr['used']['estate_id'];
        if($estate_id){
            $this->estate = getEstate($estate_id);
            //相同小区
            $where = array('status'=>1,'estate_id'=>$estate_id,'id'=>array('neq',$resArr['used']['id']));
            $this->used_estate = M('used')->where($where)->limit(10)->order('ordid desc,id desc')->select();
        }
        //获取当前分类
        $area_id = $resArr['used']['area_id'];
        $this->cate = M('area_cate')->where(array('id'=>$area_id ))->find();
        //相关地区的房源
        $where = array('status'=>1,'area_id'=>$area_id,'id'=>array('neq',$resArr['used']['id']));
        $this->used_area = M('used')->where($where)->limit(10)->order('ordid desc,id desc')->select();

        $this->used_user = getUserInfo($resArr['used']['user_id'],'');
        $this->img_list = json_decode($resArr['used']['file'],true);
        $this->explain = _setting('explain');
        $resArr['used']['title'] = $this->estate['title'].','.$resArr['used']['title'];
        $this->seoKey($resArr['used']);
        $this->display();
    }


    private function getUsed(){
        $used_id = intval($_GET['hid']) ? intval($_GET['hid']) : intval($_GET['id']);
        if(!$used_id){
            $this->error('参数不正确',U('Used/index'));
        }
        $used = M('used')->where(array('id'=>$used_id))->find();
        if(!$used['id']){
            $this->error('找不到对应的信息',U('Used/index'));
        }

        //给楼盘增加浏览次数
        $FangCms = new FangCms();
        $FangCms->setInc($used['id'],'used');

        //当前位置
        $this->nav = $FangCms->currentLocation('area_cate',array('status'=>1,'id'=>$used['area_id']));
        $this->used = $used;
        return array('used'=>$used);
    }
}